﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WiTeach.CourseManagement.Model;
using WiTeach.CourseManagement.ViewModel;
using System.Diagnostics;

using System.Configuration;
using WiTeach.Tools;
using WiTeach.CourseManagement.DAL;

namespace WiTeach.CourseManagement.View
{
    /// <summary>
    /// CourseIndex.xaml 的交互逻辑
    /// </summary>
    public partial class CourseIndex : Page
    {
        private static int pageNow = 0;
        private static int pageSize = 4;

        public CourseIndex()
        {
            InitializeComponent();
            // 修改页面背景主题
            ImageBrush brush = new ImageBrush();
            brush.Stretch = Stretch.Fill;
            brush.ImageSource = new BitmapImage(new Uri(App.backgroundImage, UriKind.RelativeOrAbsolute));
            this.Background = brush;
            int totalCount = CourseDAL.getCourseCount();
            int pageCount = (totalCount + pageSize - 1) / pageSize;
            if (pageCount >= pageSize)
            {
                this.prePageArrow.Visibility = System.Windows.Visibility.Visible;
                this.nextPageArrow.Visibility = System.Windows.Visibility.Visible;
            }
            this.courseList.ItemsSource = CourseDAL.getCurrentPagingList(0, pageSize, pageCount);

        }


        private void course_Click(object sender, RoutedEventArgs e)
        {
            // 获取数据
            // 页面跳转传值
            CourseList courseListPage = new CourseList();
            NavigationService.Navigate(courseListPage);

        }


        #region 分页事件

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void prePage(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            if (!btn.IsVisible)
                return;
            int totalCount = CourseDAL.getCourseCount();
            int pageCount = (totalCount + pageSize - 1) / pageSize;
            --pageNow;
            List<Course> courseList = CourseDAL.getCurrentPagingList(pageNow, pageSize, pageCount);
            this.courseList.ItemsSource = courseList;

        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void nextPage(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            if (!btn.IsVisible)
                return;
            int totalCount = CourseDAL.getCourseCount();
            int pageCount = (totalCount + pageSize - 1) / pageSize;
            ++pageNow;
            List<Course> courseList = CourseDAL.getCurrentPagingList(pageNow, pageSize, pageCount);
            this.courseList.ItemsSource = courseList;
        }

        #endregion 



        private void backHomeButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GetNavigationService(this).Navigate(new Uri("/WiTeach;component/DirectorySystem/View/Directory.xaml", UriKind.Relative));
        }

        private void backPreLevel(object sender, RoutedEventArgs e)
        {
            NavigationService.GetNavigationService(this).Navigate(new Uri("/WiTeach;component/DirectorySystem/View/Directory.xaml", UriKind.Relative));
        }

        /// <summary>
        /// 点击播放按钮，进入对应课时列表详情
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void courseDetail(object sender, RoutedEventArgs e)
        {
            Button lesson = (Button)sender;
            CourseList courseList = new CourseList((int)lesson.Tag);
           
            this.NavigationService.Navigate(courseList);
            //int ClassHour_ID = (int)lesson.Tag;
            //string ClassHour_Name = lesson.Content.ToString();

            //ClassIndex lessonIndexPage = new ClassIndex(ClassHour_ID, ClassHour_Name);
            //this.NavigationService.Navigate(lessonIndexPage);
            //Image clickedImage = (Image)sender;
            //// 跳转至课程对应课时列表页面
            //CourseList courseList = new CourseList((int)clickedImage.Tag);
            ////courseList.LessonID = (int)clickedImage.Tag;
            //this.NavigationService.Navigate(courseList);

        }

        private void logout_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GetNavigationService(this).Navigate(new Uri("/WiTeach;component/LoginManagement/View/Login.xaml", UriKind.Relative));
        }


        private void uv4_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string KEIL_Lic_path = System.Configuration.ConfigurationManager.AppSettings["app_KEIL_Lic"];
                Process uv4 = new Process();
                uv4.StartInfo.FileName = KEIL_Lic_path;
                uv4.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
                uv4.Start();
                uv4.WaitForExit();
                uv4.Close();
            }
            catch (Exception)
            {
            }

            //   System.Diagnostics.Process.Start(@KEIL_Lic_path);
        }

        private void progisp_Click(object sender, RoutedEventArgs e)
        {
            string progisp_path = System.Configuration.ConfigurationManager.AppSettings["app_progisp"];


            //     System.Diagnostics.Process.Start(@progisp_path);
            try
            {
                Process progisp = new Process();
                progisp.StartInfo.FileName = progisp_path;
                progisp.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
                progisp.Start();
                progisp.WaitForExit();
                progisp.Close();
                GC.Collect();
            }
            catch (Exception)
            {
            }

        }

        private void flymcu_Click(object sender, RoutedEventArgs e)
        {
            string flymcu_path = System.Configuration.ConfigurationManager.AppSettings["app_flymcu"];
            //          System.Diagnostics.Process.Start(@flymcu_path);
            try
            {
                Process flymcu = new Process();
                flymcu.StartInfo.FileName = flymcu_path;
                flymcu.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
                flymcu.Start();
                flymcu.WaitForExit();
                flymcu.Close();
                GC.Collect();
            }
            catch (Exception)
            {
            }

        }

        private void arduino_Click(object sender, RoutedEventArgs e)
        {
            string arduino_path = System.Configuration.ConfigurationManager.AppSettings["app_arduino"];
            //        System.Diagnostics.Process.Start(@arduino_path);
            try
            {
                Process arduino = new Process();
                arduino.StartInfo.FileName = arduino_path;
                arduino.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
                arduino.Start();
                arduino.WaitForExit();
                arduino.Close();
                GC.Collect();
            }
            catch (Exception)
            {
            }

        }
        private void scratch_Click(object sender, RoutedEventArgs e)
        {
            string scratch_path = System.Configuration.ConfigurationManager.AppSettings["app_scratch"];
            //        System.Diagnostics.Process.Start(@arduino_path);
            try
            {
                Process scratch = new Process();
                scratch.StartInfo.FileName = scratch_path;
                scratch.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
                scratch.Start();
                scratch.WaitForExit();
                scratch.Close();
                GC.Collect();
            }
            catch (Exception)
            {
            }

        }
        private void anaconda_Click(object sender, RoutedEventArgs e)
        {
            string anaconda_path = System.Configuration.ConfigurationManager.AppSettings["app_anaconda"];
            //        System.Diagnostics.Process.Start(@arduino_path);
            try
            {
                Process scratch = new Process();
                scratch.StartInfo.FileName = anaconda_path;
                scratch.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
                scratch.Start();
                scratch.WaitForExit();
                scratch.Close();
                GC.Collect();
            }
            catch (Exception)
            {
            }

        }
        private void sa_Click(object sender, RoutedEventArgs e)
        {
            string sa_path = System.Configuration.ConfigurationManager.AppSettings["app_S4A15"];
            Process sa = new Process();
            try
            {
                sa.StartInfo.FileName = sa_path;
                Console.WriteLine("sa_path" + sa_path);
                sa.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
                sa.Start();
                sa.WaitForExit();
                sa.Close();
                GC.Collect();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        private void course_logout_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 35;
            image.Width = 74;
        }
        private void course_logout_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 28;
            image.Width = 68;
        }
        private void course_keil_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 56;
            image.Width = 59;
        }
        private void course_keil_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 46;
            image.Width = 49;
        }
        private void course_arduino_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 54;
            image.Width = 60;
        }
        private void course_arduino_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 48;
            image.Width = 54;
        }
        
        private void course_anaconda_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 54;
            image.Width = 60;
        }
        private void course_anaconda_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 48;
            image.Width = 54;
        }
        private void course_scratch_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 54;
            image.Width = 60;
        }
        private void course_scratch_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 48;
            image.Width = 54;
        }
        private void course_flymcu_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 52;
            image.Width = 60;
        }
        private void course_flymcu_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 48;
            image.Width = 54;
        }
        private void course_progisp_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 52;
            image.Width = 59;
        }
        private void course_progisp_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 48;
            image.Width = 54;
        }
        private void course_s4a_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 53;
            image.Width = 59;
        }
        private void course_s4a_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 48;
            image.Width = 54;
        }


        private void courseindex_pagearrow_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 61;
            image.Width = 34;
        }

        private void courseindex_pagearrow_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 52;
            image.Width = 25;
        }

        private void courseindex_lesson_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Width = 50;
            image.Height = 48;
        }

        private void courseindex_lesson_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Width = 46;
            image.Height = 44;
        }
    }
}
